﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Mindfor.Web.Data
{
	/// <summary>
	/// Contains functions to operate log types. You can extend types in this class
	/// </summary>
	public static class LogTypes
	{
		static List<LogType> s_logTypes;

		/// <summary>
		/// Initializes standart types.
		/// </summary>
		static LogTypes()
		{
			s_logTypes = new List<LogType>();
			Add(0, "Message");
			Add(1, "Warning");
			Add(2, "Error");
		}

		/// <summary>
		/// Gets standart message LogType.
		/// </summary>
		public static LogType Message
		{
			get { return Get("Message"); }
		}

		/// <summary>
		/// Gets standart warning LogType.
		/// </summary>
		public static LogType Warning
		{
			get { return Get("Warning"); }
		}

		/// <summary>
		/// Gets standart error LogType.
		/// </summary>
		public static LogType Error
		{
			get { return Get("Error"); }
		}

		/// <summary>
		/// Gets all log types.
		/// </summary>
		public static LogType[] All
		{
			get { return s_logTypes.ToArray(); }
		}

		/// <summary>
		/// Gets log type by name. If not found than null is returned.
		/// </summary>
		/// <param name="name">Log type name.</param>
		public static LogType Get(string name)
		{
			string n = name.ToLower();
			return s_logTypes.FirstOrDefault(r => r.SysName == n);
		}

		/// <summary>
		/// Gets log type by id. If not found than null is returned.
		/// </summary>
		/// <param name="id">Log type id.</param>
		public static LogType Get(byte id)
		{
			return s_logTypes.FirstOrDefault(r => r.Id == id);
		}

		/// <summary>
		/// Adds new log type. You cannot add log types with standart id or name.
		/// Standart id and names:
		/// 0 - Message,
		/// 1 - Warning,
		/// 2 - Error.
		/// </summary>
		/// <param name="logType">LogType to add.</param>
		public static void Add(LogType logType)
		{
			if (s_logTypes.Any(r => r.Id == logType.Id || r.SysName == logType.SysName))
				throw new ArgumentException("LogType with this id or name already exists.");
			s_logTypes.Add(logType);
		}

		/// <summary>
		/// Adds new log type. You cannot add log types with standart id or name.
		/// Standart ids and names:
		/// 0 - Message,
		/// 1 - Warning,
		/// 2 - Error.
		/// </summary>
		/// <param name="id">LogType id.</param>
		/// <param name="name">LogType name.</param>
		public static void Add(byte id, string name)
		{
			LogType l = new LogType(id, name);
			Add(l);
		}
	}
}
